package com.ss.android.newmedia.message;

import android.content.Context;
import com.bytedance.common.utility.Logger;
import com.ss.android.common.util.MultiProcessSharedProvider;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;

public class b {
    public static final class a {
        public int a;
        public int b;
        public String c;
        public int d;
        public String e;
        public long f;
        public long g;
        public int h;
        public int i;
        public boolean j;
        public boolean k;

        public a() {

        }

        public final JSONObject a() {
            JSONObject v0 = new JSONObject();
            try {
                if(this.a <= 0) {
                    return v0;
                }

                if(android.support.design.a.isTextEmpty(this.c)) {
                    return v0;
                }

                v0.put("id", this.a);
                v0.put("type", this.b);
                v0.put("obj", this.c);
                v0.put("from", this.d);
                v0.put("extra", this.e);
                v0.put("receiverTime", this.f);
                v0.put("lastShowTime", this.g);
                v0.put("showNotificationTimes", this.h);
                v0.put("showLockTimes", this.i);
            }
            catch(Throwable v1) {
            }

            return v0;
        }

        public final String toString() {
            return "MessageObj{id=" + this.a
                    + ", type=" + this.b
                    + ", obj=\'" + this.c + '\''
                    + ", from=" + this.d
                    + ", extra=\'" + this.e + '\''
                    + ", receiverTime=" + this.f
                    + ", lastShowTime=" + this.g
                    + ", showNotificationTimes=" + this.h
                    + ", showLockTimes=" + this.i
                    + ", isCaneShowLocker=" + this.j
                    + ", isDel=" + this.k + '}';
        }
    }

    com.ss.android.newmedia.message.aa a;
    private static b b;
    private Context c;

    private b(Context arg2) {
        this.a = new com.ss.android.newmedia.message.aa();
        this.c = arg2.getApplicationContext();
        this.c();
    }

    public final void a(int arg3) {
        if(! com.ss.android.newmedia.message.c.a().e()) {
            this.a.a();
        }
        else if(arg3 > 0) {
            this.a.b(Integer.valueOf(arg3));
            this.b();
        }
    }

    public static b a(Context arg2) {
        if(b == null) {
            Class v1 = b.class;
            synchronized (v1){
                if(b == null) {
                    b = new b(arg2);
                }
            }
        }
        return b;
    }

    final List a() {
        int v0_3;
        int v3 = 0;
        LinkedList v4 = new LinkedList();
        if(! com.ss.android.newmedia.message.c.a().e()) {
            this.a.a();
            return ((List)v4);
        }

        try {
            Map v0_1 = this.a.b();
            if(v0_1.isEmpty()) {
                return ((List)v4);
            }

            v3 = 0;
            Iterator v5 = v0_1.entrySet().iterator();
            while(true) {
                if(!v5.hasNext()) {
                   break;
                }

                Object v0_2 = v5.next();
                if(v0_2 == null) {
                    continue;
                }

                Object v1 = ((Map.Entry)v0_2).getValue();
                if(com.ss.android.newmedia.message.cache.a.a().a(((a)v1))) {
                    ((List)v4).add(((Map.Entry)v0_2).getValue());
                    continue;
                }

                if(((a)v1).k) {
                    this.a.b(((Map.Entry)v0_2).getKey());
                    v0_3 = 1;
                }
                else {
                    v0_3 = v3;
                }

                v3 = v0_3;
                continue;
            }
        } catch(Exception v0) {
            v0.printStackTrace();
        }


        if(v3 != 0) {
            this.b();
        }

        if(((List)v4).size() <= 1) {
            return v4;
        }

        List v0_4 = ((List)v4).subList(((List)v4).size() - 1, ((List)v4).size());
        return v0_4;
    }

    final void b() {
        try {
            JSONArray v1 = new JSONArray();
            Map v0_1 = this.a.b();
            if(!v0_1.isEmpty()) {
                Iterator v2 = v0_1.entrySet().iterator();
                while(v2.hasNext()) {
                    Object v0_2 = v2.next();
                    if(v0_2 == null) {
                        continue;
                    }

                    v1.put(((a)((Map.Entry)v0_2).getValue()).a());
                }
            }

            String v0_3 = v1.toString();
            if(v0_3 != null) {
                Logger.d("MessageHandler", "save messageCacheListStr = " + v0_3);
            }

            com.ss.android.common.util.MultiProcessSharedProvider.a v1_1 = MultiProcessSharedProvider.a(this.c);
            v1_1.a("message_cache_list", v0_3);
            v1_1.a();
        }
        catch(Throwable v0) {
            v0.printStackTrace();
        }
    }

    private void c() {
        a v4;
        JSONObject v0_2;
        int v1;
        try {
            String v0_1 = MultiProcessSharedProvider.getMultiProcessSharedPreference(this.c).getString("message_cache_list", "");
            if(android.support.design.a.isTextEmpty(v0_1)) {
                return;
            }

            if(v0_1 != null) {
                Logger.d("MessageHandler", "load messageCacheListStr = " + v0_1);
            }

            JSONArray v2 = new JSONArray(v0_1);
            int v3 = v2.length();
            v1 = 0;
            while(true) {
            label_21:
                if(v1 >= v3) {
                    return;
                }

                v0_2 = v2.optJSONObject(v1);
                v4 = new a();
                if(v0_2 == null) {
                    label_55:
                    this.a.a(Integer.valueOf(v4.a), v4);
                    ++v1;
                    continue;
                }
                v4.a = v0_2.optInt("id");
                v4.b = v0_2.optInt("type");
                v4.c = v0_2.optString("obj");
                v4.d = v0_2.optInt("from");
                v4.e = v0_2.optString("extra");
                v4.f = ((long)v0_2.optInt("receiverTime"));
                v4.g = ((long)v0_2.optInt("lastShowTime"));
                v4.h = v0_2.optInt("showNotificationTimes");
                v4.i = v0_2.optInt("showLockTimes");
                this.a.a(Integer.valueOf(v4.a), v4);
                ++v1;
                continue;
            }
        } catch(Throwable v0) {
            v0.printStackTrace();
        }
    }
}

